<?php

namespace App\Http\Controllers\Api\Common;

use App\Http\Controllers\Controller;
use App\Models\Admin\Setting;
use App\Models\Common;
use App\Models\Member;
use App\Models\UserUni;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class PayController extends Controller
{
    public $apitype;

    public function getData(){
        $headers = getallheaders();
        if($headers){
            $notify['nonce'] = isset($headers['Wechatpay-Nonce'])?$headers['Wechatpay-Nonce']:'';
            $notify['sign'] = isset($headers['Wechatpay-Signature'])?$headers['Wechatpay-Signature']:'';
            $notify['timestamp'] = isset($headers['Wechatpay-Timestamp'])?$headers['Wechatpay-Timestamp']:'';
            $notify['serialNo'] = isset($headers['Wechatpay-Serial'])?$headers['Wechatpay-Serial']:'';
            $notify['body'] = file_get_contents('php://input');
            if($notify['nonce'] && $notify['sign'] && $notify['timestamp'] && $notify['serialNo'] && $notify['body']){
                $this->apitype='wechat';
                return $notify;
            }else{
                $notify = $_POST;
                if($notify['passback_params']){
                    $this->apitype='alipay';
                    return $notify;
                }
            }
        }
        exit('Access Denied');
    }

    public function notify(Request $request){
        $notify = $this->getData();
        if($this->apitype=='alipay'){
            $pay = app('pay.ali');
        }else if($this->apitype=='wechat'){
            $pay = app('pay.wechat');
        }

        if($pay->apitype=='alipay'){
            $result = $pay->notifyverify($notify);
            if($result===true && $notify['trade_status'] == 'TRADE_SUCCESS' && $notify['app_id']==$pay->appId && strtolower($notify['seller_id'])==strtolower($pay->seller_id)){
                $notify['origin'] = $notify['passback_params'];
                $notify['buyer'] = $notify['buyer_id'];
                $this->notify_r($pay,$notify);
            }else{
                $pay->fail();
            }
        }else if($pay->apitype=='wechat'){
            $result = $pay->validate($notify);
            if($result===false){
                $pay->fail('sign_error');
            }
            try{
                $res = $pay->decrypt($notify['body']);
            }catch(Exception $e){
                if($e->getMessage() == 'Error php>=7.1'){
                    $pay->close();
                    $pay->fail('pay_error');
                }
            }
            if ($res === false) {
                $pay->fail('pay_error');
            }
            if ($res['trade_state'] == 'SUCCESS') {
                $notify = $res;
                $notify['origin'] = $notify['attach'];
                $notify['trade_no'] = $notify['transaction_id'];
                $notify['buyer'] = $notify['payer']['openid'];
                $this->notify_r($pay,$notify);
            }
        }else{
            exit('Access Denied');
        }
    }

    function notify_r($pay,$notify){
        if(preg_match('/^sys_[a-zA-Z0-9_]+/',$notify['origin'])){
            if($notify['origin']=='sys_home_credit_order'){
                DB::beginTransaction();
                $order = C::t('home_credit_order')->fetchForUpdate($notify['out_trade_no']);
                if($notify['out_trade_no']==$order['orderid']){
                    if($order['status'] == 1) {
                        DB::commit();
                        $pay->success();
                    }
                }
                DB::rollBack();
                $pay->fail();
            }
        }else if(preg_match('/^plugin_[a-zA-Z0-9_]+/',$notify['origin'])){

        }
        $pay->fail();
    }

}
